//==============================================================================
//
// Title:       TXChainGain.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-5-9 at 19:09:54 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

#include "RFBB.h"         
#include <utility.h>
#include <formatio.h>
#include "rsnrpz.h"
#include "PowerMeter.h"
#include "USB6501.h"
#include "ReadCalXmlFile.h"                  

_DAQMX_WRITE_PORT_CMD g_szTXWritePort1Cmd[4] = {{"SFP_ON","DIO1/port1",TX1TOFB1LOOP},      
												   {"SFP_OFF","DIO1/port1",TX2TOFB2LOOP},
												   {"SFP_OFF","DIO1/port1",TX3TOFB3LOOP},
												   {"SFP_OFF","DIO1/port1",TX4TOFB4LOOP}};  

//FMON_ACTION_SYNTHESIZER                              
_SHMON_TX_CHAIN_GAIN_CMD g_szShmonTXChainGainCmd[12] ={
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_H_Pipe1.txt" ,"TX_Chain_Gain_H_Pipe1.txt","dapd -sg 1 -30","ac -I 1"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_M_Pipe1.txt" ,"TX_Chain_Gain_M_Pipe1.txt","dapd -sg 1 -30","ac -I 1"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_L_Pipe1.txt" ,"TX_Chain_Gain_L_Pipe1.txt","dapd -sg 1 -30","ac -I 1"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_H_Pipe2.txt" ,"TX_Chain_Gain_H_Pipe2.txt","dapd -sg 2 -30","ac -I 9"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_M_Pipe2.txt" ,"TX_Chain_Gain_M_Pipe2.txt","dapd -sg 2 -30","ac -I 9"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_L_Pipe2.txt" ,"TX_Chain_Gain_L_Pipe2.txt","dapd -sg 2 -30","ac -I 9"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_H_Pipe3.txt" ,"TX_Chain_Gain_H_Pipe3.txt","dapd -sg 3 -30","ac -I 17"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_M_Pipe3.txt" ,"TX_Chain_Gain_M_Pipe3.txt","dapd -sg 3 -30","ac -I 17"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_L_Pipe3.txt" ,"TX_Chain_Gain_L_Pipe3.txt","dapd -sg 3 -30","ac -I 17"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_H_Pipe4.txt" ,"TX_Chain_Gain_H_Pipe4.txt","dapd -sg 4 -30","ac -I 25"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_M_Pipe4.txt" ,"TX_Chain_Gain_M_Pipe4.txt","dapd -sg 4 -30","ac -I 25"},
        {"C:\\SWIFT\\Sources\\FRIG\\FRIG_Script\\TX_Chain_Gain_L_Pipe4.txt" ,"TX_Chain_Gain_L_Pipe4.txt","dapd -sg 4 -30","ac -I 25"}};


/**************************************************************************************************
  Function:        TXChainGain
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST TXChainGain(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    ViStatus viStatus; // For nrpErrChk -macro
    
    double dTestNum = 0.0;
    int  error = 0;
    int  iPassFail = 1;
    int i = 0;
    int iPipeNum = 0;
    int iReturn = 0;
    double dCableOffset = 0.0;
    double dOutputInDbm = 0.0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestNum", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dTestNum));
    
    //sprintf(szElementPath, "%s.%s.PARAMETERS.CableOffset", TESTPLAN_BASE_PATH, pszStepName );   
    //tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dCableOffset));

    if((dTestNum >= 0) && (dTestNum < 12))
    {
        i = dTestNum;
    }
    
    //--------------------------------------------------------------------
    iReturn = RfbbInetFTPAutoSend(g_szDUTIP, "", "", g_szShmonTXChainGainCmd[i].cLocalFilePath, g_szShmonTXChainGainCmd[i].cRemoteFileName, INET_FTP_FILE_TYPE_BINARY);    
    if(iReturn < 0)
    {
        sprintf( szLogMsg, STRING_MESSAGE_Ftp_File_Err, g_szDUTIP, InetGetErrorMessage( iReturn ) );
        EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
        
        goto Error;
    } 

    g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 

    //run script    
    sprintf(kszShmonAgent,". %s",g_szShmonTXChainGainCmd[i].cRemoteFileName);
    Shmon_RunScript(kszShmonAgent,5);

    // Setup PowerMeter offset and frequency
    nrpErrChk( rsnrpz_corr_setOffset(g_hPowerMeter, 1, 0 ));//2.5
    nrpErrChk( rsnrpz_chan_setCorrectionFrequency( g_hPowerMeter, 1, 1e6*2130 ) );

    error = MeasurePower( 1, 1, 0.2, &dOutputInDbm );
    if( error  < 0 )
    {
       sprintf(errMsg, "Error: MeasurePower failed!");
       ErrChk( EELog_INEventLogLogEvent (g_hEventLog, NULL, errMsg) ); 
       goto Error;
    }

    dOutputInDbm = dOutputInDbm + dTXPWMCalData[i];
    sprintf(szLogMsg,"%s power is %f dBm",g_szShmonTXChainGainCmd[i].cRemoteFileName,dOutputInDbm)   ;
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    //inactive Carry
    sprintf(kszShmonAgent,g_szShmonTXChainGainCmd[i].cSetDapd);
    ShellCommand_String(kszShmonAgent, -1,"successfully", &iPassFail);
    
    sprintf(kszShmonAgent,g_szShmonTXChainGainCmd[i].cInactiveCarry);
    ShellCommand_String(kszShmonAgent, -1,"StatusOk", &iPassFail); 
    
Error:
    
#if(ON_OFF != SIMU_VERS) 

    testData->measurement = dOutputInDbm;

    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }

    InetTelnetClose(g_hShellCommand); 
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}


/**************************************************************************************************
  Function:        TXChainGainInit
  Description:     This test is the tests the lock detect status of all synthesizers.
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID:    FAHA_DigTRX_BIST_SYNTH
  Used Command:    g_szCmonSynOfLock
**************************************************************************************************/
void __decldllexp TX_TEST TXChainGainInit(tTestData *testData, tTestError *testError)
{

    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};   
    char szElementPath[BUFFER_SIZE_256] ={'\0'};
    double dTestNum = 0.0;
    int  error = 0;
    int  iPassFail = 1;
    int i = 0;
    int iPipeNum = 0;
    int iReturn = 0;
     char szOutputMessage[BUFFER_SIZE_256] = {'\0'};
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    // Get Test Parameters from Sequence FileGlobals    
    sprintf(szElementPath, "%s.%s.PARAMETERS.TestNum", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValNumber(testData->seqContextCVI, &errorInfo, szElementPath, 0, &dTestNum));

    if((dTestNum >= 0) && (dTestNum < 4))
    {
        i = dTestNum;
    }   

     //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Connect the TX%d cable #####",i);
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
       
    // FiberOptic Connect With Dut
    ErrChk(USBWriteDigToPort(g_szTXWritePort1Cmd[i].ucTashName,g_szTXWritePort1Cmd[i].ucDevport,g_szTXWritePort1Cmd[i].uiWriteData)); 
    
    //sprintf(szOutputMessage,"please connect the TX RF-cable on Pipe%d",(i + 1));
    //MessagePopup("TX Chain Gain Test", szOutputMessage);
    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    {
        testData->result = PASS;
    }
    else 
    {
        testData->result = FAIL;
    }

    //InetTelnetClose(g_hShellCommand); 
    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}

